package org.wenhuayuan.project.main.http

import org.wenhuayuan.library.resful.HiResful
import org.wenhuayuan.project.common.util.SPUtil

object ApiFactory {
    private const val KEY_DEGRADE_HTTP = "degrade_http"
    private const val HTTPS_BASE_URL = "https://api.devio.org/as/"
    private const val HTTP_BASE_URL = "http://api.devio.org/as/"

    private val degrade2Http = SPUtil.getBoolean(KEY_DEGRADE_HTTP)
    private val baseUrl = if (degrade2Http) HTTP_BASE_URL else HTTPS_BASE_URL


    private val hiRestful: HiResful = HiResful(baseUrl,
        RetrofitCallFactory(baseUrl)
    )

    init {
        hiRestful.addInterceptor(BizInterceptor())
//        hiRestful.addInterceptor(HttpStatusIntercepter())

        SPUtil.putBoolean(KEY_DEGRADE_HTTP, false) // 主动还原
    }

    fun <T> create(service: Class<T>): T {
        return hiRestful.create(service)
    }
}